//
//  THJEditUserInfoVC.m
//  THJXHW
//
//  Created by 河南德艺 on 17/3/23.
//  Copyright © 2017年 河南德艺. All rights reserved.
//

#import "THJEditUserInfoVC.h"
#import "ZBNetworking.h"

@interface THJEditUserInfoVC ()
{
    NSInteger genderFlag;
}
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topSpace;

@end

@implementation THJEditUserInfoVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewDidLoad{
    [super viewDidLoad];
    genderFlag = 0;
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"个人信息"];
    }else{
        [self setNavBarWithTitle:@"个人信息"];
    }
    [self getData];
    self.topSpace.constant = NavbarHeight;
}
-(void)getData{
    __weak typeof(self) weekself = self;
    NSString * urlstr = [NSString stringWithFormat:@"%@%@",THJUrlDomain,@"index.php?f=member&r=member&m=get_info"];
    NSString * token = [THJTools getToken];
    [MBProgressHUD showMessage:@"" toView:self.view];
    [ZBRequestManager requestWithConfig:^(ZBURLRequest *request){
        request.urlString=urlstr;
        request.apiType=ZBRequestTypeRefresh;
        [request setValue:token forHeaderField:@"token"];
    }  success:^(id responseObject,apiType type){
        NSDictionary * receiveData = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        [MBProgressHUD hideHUDForView:weekself.view animated:YES];
        NSLog(@"个人信息-%@",receiveData);
        if([receiveData[@"status"] isEqual:@"200"]){
            weekself.tf1.text = receiveData[@"data"][@"truename"];
            if([receiveData[@"data"][@"gender"] isEqual:@"1"]){
                [weekself setBtnSelected:weekself.btn2];
            }else if ([receiveData[@"data"][@"gender"] isEqual:@"2"]){
                [weekself setBtnSelected:weekself.btn3];
            }else if ([receiveData[@"data"][@"gender"] isEqual:@"0"]){
                [weekself setBtnSelected:weekself.bt1];
            }
             weekself.phoneLab.text = receiveData[@"data"][@"mobile"];
        }
       
    } failed:^(NSError *error){
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        NSLog(@"error-%@",error);
    }];
}

- (IBAction)xiugaiAction:(id)sender {
    if(!self.tf1.text.length){
        [self HUDShowMsg:@"请填写真实姓名" AfterDelayDimiss:1];
        return;
    }
    NSString * url = [NSString stringWithFormat:@"%@index.php?f=member&r=member&m=save",THJUrlDomain];
    NSDictionary * dic = @{@"truename":self.tf1.text,
                           @"gender":[NSString stringWithFormat:@"%ld",genderFlag],
                           };
    [THJNetWork postDataFromUrl:url parameters:dic Token:[THJTools getToken] callback:^(id receiveData, NSError *error) {
        [self HUDShowMsg:receiveData[@"tip"] AfterDelayDimiss:1];
        if([receiveData[@"status"] isEqual:@"200"]){
            [self performSelector:@selector(popSelf) withObject:nil afterDelay:1];
        }
    }];
}
-(void)popSelf{
    [self.delegate changeTruename:self.tf1.text];
    [self pop];
}
- (IBAction)btn1Action:(UIButton*)sender {
    genderFlag = 0;
    [self setBtnSelected:sender];
}
- (IBAction)btn2Action:(UIButton*)sender {
    genderFlag = 1;
    [self setBtnSelected:sender];
}
- (IBAction)btn3Action:(UIButton*)sender {
    genderFlag = 2;
    [self setBtnSelected:sender];
}

-(void)setBtnSelected:(UIButton*)btn{
   
     [self.btn2 setImage:kGetImage(@"圈") forState:UIControlStateNormal];
     [self.btn3 setImage:kGetImage(@"圈") forState:UIControlStateNormal];
     [self.bt1 setImage:kGetImage(@"圈") forState:UIControlStateNormal];
     [btn setImage:kGetImage(@"对号") forState:UIControlStateNormal];
}
@end
